//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "PathPayoff.h"
using namespace Cephei::QL::Experimental::Mcbasket;
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Math/Array.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Termstructures;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Mcbasket::CPathPayoff::CPathPayoff (boost::shared_ptr<QuantLib::PathPayoff>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathPayoff = NULL;
#endif
	_ppPathPayoff = &childNative;
    
}
Cephei::QL::Experimental::Mcbasket::CPathPayoff::CPathPayoff (QuantLib::PathPayoff& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPathPayoff = NULL;
#endif
	_ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (&childNative);
    
    _PathPayoffOwner = owner;
    
}

Cephei::QL::Experimental::Mcbasket::CPathPayoff::CPathPayoff (CPathPayoff^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathPayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Mcbasket::CPathPayoff::CPathPayoff (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathPayoff = NULL;
#endif
	if (!t->IsSubclassOf(CPathPayoff::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Mcbasket::CPathPayoff::CPathPayoff (QuantLib::Handle<QuantLib::PathPayoff>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPathPayoff = &childNative;
	_ppPathPayoff = &static_cast<boost::shared_ptr<QuantLib::PathPayoff>>(childNative.currentLink());
    
    _PathPayoffOwner = owner;
}
Cephei::QL::Experimental::Mcbasket::CPathPayoff::CPathPayoff (QuantLib::Handle<QuantLib::PathPayoff> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPathPayoff = &childNative;
	_ppPathPayoff = &static_cast<boost::shared_ptr<QuantLib::PathPayoff>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Mcbasket::CPathPayoff::CPathPayoff (QuantLib::PathPayoff childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPathPayoff = NULL;
#endif
	_ppPathPayoff = new boost::shared_ptr<QuantLib::PathPayoff> (new QuantLib::PathPayoff (childNative));
    
}
#endif

Cephei::QL::Experimental::Mcbasket::CPathPayoff::~CPathPayoff ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPathPayoff != NULL)
    {
	    delete _ppPathPayoff;
        _ppPathPayoff = NULL;
    }
}
Cephei::QL::Experimental::Mcbasket::CPathPayoff::!CPathPayoff ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPathPayoff != NULL)
    {
	    delete _ppPathPayoff;
    }
}
QuantLib::PathPayoff& Cephei::QL::Experimental::Mcbasket::CPathPayoff::GetReference ()
{
    if (_ppPathPayoff == NULL) throw REFNEW NativeNullException ();
	return **_ppPathPayoff;
}
boost::shared_ptr<QuantLib::PathPayoff>& Cephei::QL::Experimental::Mcbasket::CPathPayoff::GetShared ()
{
    if (_ppPathPayoff == NULL) throw REFNEW NativeNullException ();
	return *_ppPathPayoff;
}
QuantLib::PathPayoff* Cephei::QL::Experimental::Mcbasket::CPathPayoff::GetPointer ()
{
    if (_ppPathPayoff == NULL) throw REFNEW NativeNullException ();
	return &**_ppPathPayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PathPayoff>& Cephei::QL::Experimental::Mcbasket::CPathPayoff::GetHandle ()
{
	if (_phPathPayoff == NULL)
	{
		_phPathPayoff = new Handle<QuantLib::PathPayoff> (*_ppPathPayoff);
	}
	return *_phPathPayoff;
}
#endif
bool Cephei::QL::Experimental::Mcbasket::CPathPayoff::HasNative () 
{
	return (_ppPathPayoff != NULL);
}

String^ Cephei::QL::Experimental::Mcbasket::CPathPayoff::Description::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppPathPayoff)->description ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Experimental::Mcbasket::CPathPayoff::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppPathPayoff)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Experimental::Mcbasket::CPathPayoff::BasisSystemDimension::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppPathPayoff)->basisSystemDimension ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Mcbasket::IPathPayoff^ Cephei::QL::Experimental::Mcbasket::CPathPayoff::Value (Cephei::QL::Math::IMatrix^ path, Cephei::Core::IVector<Cephei::QL::Termstructures::IYieldTermStructure^>^ forwardTermStructures, Cephei::QL::Math::IArray^ payments, Cephei::QL::Math::IArray^ exercises, Cephei::Core::IVector<Cephei::QL::Math::IArray^>^ states)
{
    CMatrix^ _Cpath;
    CArray^ _Cpayments;
    CArray^ _Cexercises;
    try
    {
        _Cpath = safe_cast<CMatrix^> (path);
        _Cpath->Lock();
        QuantLib::Matrix& _path = static_cast<QuantLib::Matrix&> (_Cpath->GetReference ()); 
        forwardTermStructures->Lock();
        INativeVector<Cephei::QL::Termstructures::IYieldTermStructure^>^ _NCIforwardTermStructures = forwardTermStructures->getFeature (NativeFeature::Handle);
        CYieldTermStructureVector^ _NCforwardTermStructures = safe_cast<CYieldTermStructureVector^>(_NCIforwardTermStructures);
        std::vector<Handle<QuantLib::YieldTermStructure> >& _forwardTermStructures = static_cast<std::vector<Handle<QuantLib::YieldTermStructure> >&> (_NCforwardTermStructures->GetHandle ());
        _Cpayments = safe_cast<CArray^> (payments);
        _Cpayments->Lock();
        QuantLib::Array& _payments = static_cast<QuantLib::Array&> (_Cpayments->GetReference ()); 
        _Cexercises = safe_cast<CArray^> (exercises);
        _Cexercises->Lock();
        QuantLib::Array& _exercises = static_cast<QuantLib::Array&> (_Cexercises->GetReference ()); 
        states->Lock();
        INativeVector<Cephei::QL::Math::IArray^>^ _NCIstates = states->getFeature (NativeFeature::Value);
        CArrayVector^ _NCstates = safe_cast<CArrayVector^>(_NCIstates);
        std::vector<QuantLib::Array>& _states = static_cast<std::vector<QuantLib::Array>&> (_NCstates->GetReference ());
    	(*_ppPathPayoff)->value ( _path,  _forwardTermStructures,  _payments,  _exercises,  _states );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cpath != nullptr) _Cpath->Unlock();
        if (forwardTermStructures != nullptr) forwardTermStructures->Unlock(); //NO2  
        if (_Cpayments != nullptr) _Cpayments->Unlock();
        if (_Cexercises != nullptr) _Cexercises->Unlock();
        if (states != nullptr) states->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

